<template>
    <div>
        <div class="goods">
            <div class="menu_wrapper" ref="menu">
                <ul>
                    <li v-for="(t,index) in goods" class="menu_item" :class="{'current':currentIndex===index}" @click="selectMenu(index,$event)">
                        <span class="text">
                            <span v-show="t.type>0">^</span>{{t.name}}</span>
                    </li>
                </ul>
            </div>
            <div class="foods_wrapper" ref="food">
                <ul>
                    <li v-for="pt in goods" class="food_list food_list_height">
                        <h1 class="title">{{pt.name}}</h1>
                        <ul>
                            <li v-for="t in pt.foods" class="food_item" @click="selectFood(t,$event)">
                                <div class="icon">
                                    <img :src="t.icon" width="57px" height="57px">
                                </div>
                                <div class="content">
                                    <h2 class="name">{{t.name}}</h2>
                                    <p class="desc">{{t.description}}</p>
                                    <div class="extra">
                                        <span class="count">月售{{t.sellCount}}</span>
                                        <span>好评率{{t.rating}}%</span>
                                    </div>
                                    <div class="price">
                                        <span class="now">￥{{t.price}}</span>
                                        <del class="old" v-show="t.oldPrice">￥{{t.oldPrice}}</del>
                                    </div>
                                    <div class="cartcontrol_wrapper">
                                        <cartcontrol :food="t" @add="addFood"></cartcontrol>
                                    </div>
                                </div>
                            </li>
                        </ul>
                    </li>
                </ul>
            </div>
            <shopcart ref="shopcart" :deliveryPrice="seller.deliveryPrice" :minPrice="seller.minPrice" :selectFoods="selectFoods"></shopcart>
        </div>
        <food :food="selectedFood" ref="foodComponent"></food>
    </div>
</template>

<script>
import BScroll from "better-scroll";
import shopcart from "@/components/shopcart/shopcart";
import cartcontrol from "@/components/cartcontrol/cartcontrol.vue";
import food from "@/components/food/food.vue";
const ERR_OK = 0;
export default {
  props: {
    seller: {
      type: Object
    }
  },
  data() {
    return {
      goods: [],
      listHeight: [],
      scrollY: 0,
      selectedFood: {}
    };
  },
  computed: {
    currentIndex() {
      for (let i = 0; i < this.listHeight.length; i++) {
        let height1 = this.listHeight[i];
        let height2 = this.listHeight[i + 1];
        if (!height2 || (this.scrollY >= height1 && this.scrollY < height2)) {
          return i;
        }
      }
      return 0;
    },
    selectFoods() {
      let foods = [];
      this.goods.forEach(good => {
        good.foods.forEach(food => {
          if (food.count) {
            foods.push(food);
          }
        });
      });
      return foods;
    }
  },
  created() {
    this.$ajax({
      method: "get",
      url: "/api/goods"
    })
      .then(res => {
        if (res.data.errno == ERR_OK) {
          this.goods = res.data.data;
          this.$nextTick(function() {
            this._initScroll();
            this._calculateHeight();
          });
        }
      })
      .catch(err => {
        console.log(err);
      });
  },
  methods: {
    _initScroll() {
      this.menuScroll = new BScroll(this.$refs.menu, {
        click: true
      });

      this.foodsScroll = new BScroll(this.$refs.food, {
        click: true,
        probeType: 3
      });
      this.foodsScroll.on("scroll", pos => {
        this.scrollY = Math.abs(Math.round(pos.y));
      });
    },
    _calculateHeight() {
      let foodList = this.$refs.food.getElementsByClassName("food_list_height");
      let height = 0;
      this.listHeight.push(height);
      for (let i = 0; i < foodList.length; i++) {
        let item = foodList[i];
        height += item.clientHeight;
        this.listHeight.push(height);
      }
    },
    selectMenu(index, e) {
      if (e._constructed) {
        let el = this.$refs.food.getElementsByClassName("food_list_height")[
          index
        ];
        this.foodsScroll.scrollToElement(el, 500);
      }
    },
    addFood(target) {
      this.$nextTick(() => {
        this.$refs.shopcart.drop(target);
      });
    },
    selectFood(food, e) {
      if (e._constructed) {
        this.selectedFood = food;
        this.$refs.foodComponent.show();
      }
    }
  },
  components: {
    shopcart,
    cartcontrol,
    food
  }
};
</script>

<style scoped>
.goods {
  display: flex;
  position: absolute;
  top: 126px;
  bottom: 46px;
  width: 100%;
  overflow: hidden;
}

.menu_wrapper {
  flex: 0 0 80px;
  width: 80px;
  background: #f3f5f7;
}

.menu_item {
  display: table;
  height: 54px;
  width: 56px;
  line-height: 14px;
  padding: 0 12px;
}

.menu_item.current {
  background: #fff;
}

.menu_item .text {
  display: table-cell;
  width: 56px;
  vertical-align: middle;
  font-size: 12px;
  border-bottom: 1px solid #dbdbdb;
}

.foods_wrapper {
  flex: 1;
}

.foods_wrapper .title {
  padding-left: 14px;
  height: 26px;
  line-height: 26px;
  border-left: 2px solid #d9dde1;
  font-size: 12px;
  color: rgb(147, 153, 159);
  background: #f3f5f7;
}

.food_item {
  display: flex;
  margin: 18px;
  padding-bottom: 18px;
  border-bottom: 1px solid #dbdbdb;
  position: relative;
}

.food_item .icon {
  width: 57px;
  height: 57px;
  flex: 0 0 57px;
  margin-right: 10px;
}

.food_item .content {
  flex: 1;
}

.food_item .content .name {
  margin: 2px 0 8px 0;
  height: 14px;
  line-height: 14px;
  font-size: 14px;
  color: rgb(7, 17, 27);
}

.food_item .content .desc,
.food_item .content .extra {
  line-height: 10px;
  font-size: 10px;
  color: rgb(147, 153, 159);
}

.food_item .content .desc {
  margin-bottom: 8px;
  line-height: 12px;
}

.food_item .content .extra .count {
  margin-right: 12px;
}

.food_item .content .price {
  font-weight: 700;
  line-height: 24px;
}

.food_item .content .price .now {
  color: red;
  margin-right: 8px;
  font-size: 14px;
}

.food_item .content .price .old {
  font-size: 10px;
  color: rgb(147, 153, 159);
}

.food_item:last-child {
  border-bottom: none;
  margin-bottom: 0;
}

.cartcontrol_wrapper {
  position: absolute;
  right: 0;
  bottom: 12px;
}
</style>
